package com.yuneec.droneservice.request;

import com.yuneec.droneservice.command.MAV_RC_ERROR_CODE;
import com.yuneec.droneservice.tools.RCLog;

/**
 * Created by zd.zhang on 2017/10/31.
 */

public abstract class CommonResponseHandler implements IResponseHandler {

    private IResponse response;
    private OnRequestListener listener;

    public void initResponseHandler(IResponse response, OnRequestListener listener) {
        this.response = response;
        this.listener = listener;
    }

    @Override
    public void run() {
        if (response == null || listener == null) {
            return;
        }
        try {
            if (response.getCode() == MAV_RC_ERROR_CODE.MAV_RC_ERR_SUCCESS) {
                this.listener.onSuccess(response);
            } else {
                this.listener.onError(response.getCode());
            }
        } catch (Exception e) {
            this.listener.onError(MAV_RC_ERROR_CODE.MAV_RC_ERR_UNKNOW);
            RCLog.e(String.format("handle response(id: %d ) error:%s", response.ids(), e.getMessage()));
        }
    }
}
